//
//  LHUserVIew.m
//  Knight_Tour
//
//  Created by dlios on 15-5-27.
//  Copyright (c) 2015年 TouchOne. All rights reserved.
//

#import "LHUserVIew.h"
#import "LHUserTableViewCell.h"
#import "LHUserOtherTableViewCell.h"

#define kImageWidth LHWidth

#define kImageHeight kImageWidth * 0.625
@implementation LHUserVIew

- (void)dealloc
{
    [_tableView release];
    [_userArr release];
    [_imageArr release];
    [_image1 release];
    [_lable2 release];
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.userArr = [@[@"我的游记", @"我的景点", @"我的推荐景点", @"我的旅店", @"我的餐馆", @"清除缓存", @"关于我们"]mutableCopy];
        self.imageArr = [@[@"coffe4",@"coffe1",@"coffe03",@"coffe5", @"coffe",@"coffe6",@"coffe2"] mutableCopy];
        [self createTableView];
    }
    return self;
}

- (void)createTableView
{

    
    self.headView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kImageWidth, kImageHeight)];
    _headView.image = [UIImage imageNamed:@"01"];
    _headView.contentMode = UIViewContentModeScaleAspectFill;
    [self addSubview:_headView];
    [_headView release];
    
    self.image1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, LHWidth / 7, LHWidth / 7)];
    _image1.image = [UIImage imageNamed:@"02"];
    _image1.center = CGPointMake(self.headView.frame.size.width / 2, self.headView.frame.size.height / 4 * 2);
    [_image1.layer setCornerRadius:25];
    [self.headView addSubview:_image1];
    [_image1 release];
    
    self.lable2 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width / 5 * 4, 20)];
    _lable2.text =@"记录旅行的足迹,丰富世界的风景";
    _lable2.center = CGPointMake(self.frame.size.width / 2, self.image1.frame.size.height + 110);
    [_lable2 setTextColor:[UIColor colorWithRed:62 % 256 / 255.0 green:62 % 256 / 255.0   blue:62 % 256 / 255.0 alpha:1] ];
    _lable2.textAlignment = 1;
    [self.headView addSubview:_lable2];
    [_lable2 release];
    
    
    
    
    self.tableView = [[UITableView alloc] initWithFrame:self.bounds style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor clearColor];
    [self addSubview:_tableView];
    [_tableView release];
    
    [self.tableView registerClass:[LHUserOtherTableViewCell class] forCellReuseIdentifier:@"cell1"];
    [self.tableView registerClass:[LHUserTableViewCell class] forCellReuseIdentifier:@"cell"];
    self.tableView.contentInset = UIEdgeInsetsMake(kImageHeight, 0, 64, 0);
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 2 || indexPath.row == 3) {
        LHUserOtherTableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:@"cell1" forIndexPath:indexPath];
        cell1.lable1.text = _userArr[indexPath.row];
        cell1.image1.image = [UIImage imageNamed:_imageArr[indexPath.row]];
        return cell1;
    } else {
        LHUserTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
        cell.lable1.text = _userArr[indexPath.row];
        cell.image1.image = [UIImage imageNamed:_imageArr[indexPath.row]];
        return cell;
    }
    return nil;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _userArr.count;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.delegate jumpToDetailPage:indexPath];
    [_tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

// 5.重写scrollView的协议方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    // 向下拽了多少距离
    CGFloat down = - (kImageHeight * 0.5) - scrollView.contentOffset.y;
    if (down < 0) return;
    
    CGRect frame = self.headView.frame;
    // 2 决定图片变大的速度,值越大,速度越快
    frame.size.height = kImageHeight + down/2;
    self.headView.frame = frame;

}

@end
